package com.wenbai.servicelightluxurystickers.service.impl;

import com.wenbai.servicelightluxurystickers.entity.StickManage;
import com.wenbai.servicelightluxurystickers.entity.dto.StickManageLoginDto;
import com.wenbai.servicelightluxurystickers.exception.AccountNotFoundException;
import com.wenbai.servicelightluxurystickers.exception.PasswordErrorException;
import com.wenbai.servicelightluxurystickers.mapper.StickManageMapper;
import com.wenbai.servicelightluxurystickers.service.StickManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class StickManageServicelmpl implements StickManageService {

    @Autowired
    private StickManageMapper stickManageMapper;

    /**
     * 管理员登录
     * @param stickManageLoginDto
     * @return
     */
    public StickManage login(StickManageLoginDto stickManageLoginDto) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        String account = stickManageLoginDto.getAccount();
        String password = stickManageLoginDto.getPassword_hash();

        //1根据账号查询数据库中的数据
        StickManage manage = stickManageMapper.getByAccount(account);

        if (manage == null) {
            //账号不存在
            throw new AccountNotFoundException("账号不存在");
        }

        //密码对比,此处没对密码解密
        if (!passwordEncoder.matches(password, manage.getPassword_hash())){
            //密码错误
            throw new PasswordErrorException("密码错误");
        }

        //返回实体对象
        return manage;
    }
}
